#!/usr/bin/perl -w

use dba;
use String::CRC32;
use strict;

my @known=map"`$_`",qw/McUpd id ver name code size/;
#			($id,$ver,$name,$cod,$sz)

my $table=shift(@known);
my $pv=join ',',@known;
my $pq=join ',',map'?',@known;
my $upd=$dba::h->prepare(qq/INSERT ignore $table ($pv) VALUES ($pq)/);
my $get=$dba::h->prepare(qq/SELECT size,code,name FROM $table where ver>=?/);

my $DIR='inc';

chdir "/opt/McAfee/pool";
my $ver=`script/ver2.pl SelfUpd/DAT/DAT`;
die "Unknown version:\n$ver\n" unless $ver=~/^(\d{4}) /m;
$ver=$1;

=head
Ver  Day   Vir
5310 2008-06-04 397574 UNP CLEAN.DAT
5310 2008-06-04 397574 UNP NAMES.DAT
5310 2008-06-04 397574 UNP SCAN.DAT
=cut

our $mfile;
our %h1=();
my %url=();

sub validate_ini($)
{
	open S,'<'.$_[0] or return 0;
	binmode S;
	my $sum=crc32(*S,-1) ^ -1;
	close(S);
	return $sum;
}

sub ini_load()
{
#    my $mfile=$mfile;
    open DATA,'<'.$mfile or die "$mfile: $!";
    my $sect='';%h1=();
    $/="\n";
    
#    print 'For ',$mfile;
    while ($_=<DATA>)
    {
#	print;
	
	next if /^[:;\r\n]/;
	s/;.*$//s;s/\r?\n$//;
	if (/^\[(.*)\]$/)
	{
#	    print("Content '$sect': ",join(',',$h1{$sect}),"\n");
	    $sect=$1;
#	    print("Found '$sect'\n");
	    next
	}

	my ($key,$val)=split /\s*=\s*/,$_,2;
#	print "$sect,$key,$val";
	$url{$val}=$mfile if $key eq 'UpdateUrl';
	$h1{$sect}{$key}=$val;
    }
    close DATA;
#    print "$sect=$hash\n";
#    return \%h1;
}
$\="\n";
$,="\t";

foreach $mfile qw/delta.ini/
{
#    print 'For ',$mfile;

    ini_load();

#    printf "File: $file, Keys: %u\n", scalar keys %h1;
#    printf "$_=%u\n",scalar keys %{$h1{$_}} foreach sort keys %h1;
    my %m=%{$h1{'Multiple Patch Table'}};
    my %s=%{$h1{'File Size'}};
    my %u=%{$h1{'Incremental Resolver'}};
    my %c=%{$h1{'Patch Validation'}};

    foreach (sort keys %m)
    {
	my $ver=$_;
	my $id=$m{$ver};
	my $name=$u{$id};
	$name=$& if $name=~m{[^/]*$};
	my ($cod,$sz)=($c{$name},$s{$name});
###	print $id,$ver,$name,$cod,$sz;
#	print "Found $ver\n";
	$upd->execute($id,$ver,$name,$cod,$sz);
#	print "Key: '$_'='$t{$_}'\n"
    }

$get->execute($ver);
my $up=0;
while (my ($sz,$sum,$nam)=$get->fetchrow_array())
{
    $up++;
    my $file="$DIR/$nam";
    my $sz2=-s $file or 0;        
    my $sum2=validate_ini($file) or 0;

    my $sig='#';
    $sig.=$sig unless $sz==$sz2 && $sum==$sum2;
#   print STDERR $file,$sz,$sz2,$sum,$sum2,$sz==$sz2 && $sum==$sum2;
    print($nam.$sig.$sz);
    chmod 0644,$file unless (stat _)[2] & 0111 == 0111;
}
printf $ver unless $up;

=head
open IN,'sums.lst';
my %has=();
while (<IN>)
{
#Mod16,Size,CRC,SHA1,Name
    chomp;
    next if /^#/;
    my ($mt,$size,$crc,$sha,$name)=split /,/,$_;
    $has{$name}="$size/$crc";
}

while (my ($sz,$sum,$nam)=$get->fetchrow_array())
{
#    my $file="$DIR/$nam";
#    my $sz2=-s $file || 0;        
#    my $sum2=validate_ini($file);
    
#    print STDERR $file,$sz,$sz2,$sum,$sum2,$sz==$sz2 && $sum==$sum2;
    my $id=sprintf "%08u/%08X",$sz,$sum;
    
    print($nam.'#'.$sz),next
    unless $has{$nam} eq $id;
    
#    unless $sz==$sz2 && $sum==$sum2;
#    chmod 0644,$file unless (stat _)[2] & 0111 == 0111;
}
=cut

$get->finish();

=head
[Contents]
[Multiple Patch Table] %m
4407=364

[Incremental Resolver] %u
364=44074408.upd

[Single Patch Table]

[Patch Validation] %c
44074408.upd=2415686301

[File Size] %s
44074408.upd=3435
=cut        
}
